//
//  YDASearchResultCell.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/27.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDASearchResultCell.h"
#import "YDASearchModel.h"


@interface YDASearchResultCell()

//分割线
@property (nonatomic , weak) UIView *lineView;
//股票代码
@property (nonatomic , weak) UILabel *stockCode;
//股票名称
@property (nonatomic , weak) UILabel *stockName;
//加自选按钮
@property (nonatomic , weak) SYNoHeightedButton *addOptionButton;


@end

@implementation YDASearchResultCell

-(void)setModel:(YDASearchModel *)model
{
    _model = model;
    self.stockCode.text = model.code;
    
    self.stockName.text = model.name;
    
    
    self.addOptionButton.selected = model.favorite;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *aSearchResultCell = @"YDASearchResultCell";
    YDASearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:aSearchResultCell];
    if (cell == nil) {
        cell = [[YDASearchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aSearchResultCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        
        [self lineView];
        [self stockCode];
        [self stockName];
        
        [self addOptionButton];
        
    }
    return self;
}
//分割线
-(UIView *)lineView
{
    if (_lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        [self.contentView addSubview:lineView];
        lineView.backgroundColor = NumberColor(247, 247, 247);
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(rate375(20));
            make.right.equalTo(self).offset(-rate375(20));
            make.bottom.equalTo(self);
            make.height.equalTo(@1);
        }];
        _lineView = lineView;
    }
    return _lineView;
}
//股票代码
-(UILabel *)stockCode
{
    if (_stockCode == nil) {
        UILabel *stockCode = [[UILabel alloc] init];
        [self.contentView addSubview:stockCode];
        stockCode.textColor = NumberColor(104, 114, 132);//104 114 132
        stockCode.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        stockCode.textAlignment = NSTextAlignmentLeft;
        stockCode.text = @"000001";
        [stockCode mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(rate375(20));
            make.centerY.equalTo(self);
        }];
        _stockCode = stockCode;
    }
    return _stockCode;
}
//股票名称
-(UILabel *)stockName
{
    if (_stockName == nil) {
        UILabel *stockName = [[UILabel alloc] init];
        [self.contentView addSubview:stockName];
        stockName.textColor = NumberColor(62, 89, 167);
        stockName.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        stockName.textAlignment = NSTextAlignmentLeft;
        stockName.text = @"上海物贸";
        [stockName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stockCode.mas_right).offset(rate375(20));
            make.centerY.equalTo(self);
        }];
        _stockName = stockName;
    }
    return _stockName;
}
//加自选按钮
-(SYNoHeightedButton *)addOptionButton
{
    if (_addOptionButton == nil) {
        SYNoHeightedButton *addOptionButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:addOptionButton];
        addOptionButton.backgroundColor = [UIColor clearColor];
        [addOptionButton setBackgroundImage:[UIImage imageNamed:@"gpcx-jiahao"] forState:UIControlStateNormal];
        [addOptionButton setBackgroundImage:[UIImage imageNamed:@"gpcx-jianhao"] forState:UIControlStateSelected];
        [addOptionButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-rate375(25));
            if (isPhoneX) {
                make.width.height.equalTo(@(rateX812(20)));
            }else{
                make.width.height.equalTo(@(rate750(20)));
            }
        }];
        [addOptionButton addTarget:self action:@selector(addOptionButtonClick:) forControlEvents:UIControlEventTouchDown];
        _addOptionButton = addOptionButton;
    }
    return _addOptionButton;
}
-(void)addOptionButtonClick:(SYNoHeightedButton *)button
{
    if ([self.delegate respondsToSelector:@selector(aSearchResultCellClickButton:)]) {
        [self.delegate aSearchResultCellClickButton:self];
    }
}

@end
